package com.niit.spark.rdd.test

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * Date:2025/5/9
 * Author：Ys
 * Description:
 */
object FlatMapReduceByKeyExercise {

  def main(args: Array[String]): Unit = {
    val sparkConf = new
        SparkConf().setMaster("local[*]").setAppName("FlatMapReduceByKeyExercise")
    val sc = new SparkContext(sparkConf)
    val textRDD = sc.parallelize(Seq("Hello Spark Hello World", "Spark is great",
      "Hello again Spark"))


    val resRdd: RDD[(String, Int)] = textRDD.flatMap(_.split(" ")).map(word => {
      (word, 1)
    }).reduceByKey(_ + _)

    resRdd.collect().foreach(println)

    sc.stop()
  }

}
